package Year21_Month11.day1104;

public class Solution {
    public ListNode detectCycle(ListNode head) {
        //首先判断是否有环
        if(head == null || head.next == null){
            return null;
        }
        ListNode fast = head;
        ListNode slow = head;
        ListNode key1 = head;
        ListNode key2 = null;
        while (fast !=null && fast.next != null){
            fast = fast.next.next;
            slow = slow.next;
            if(slow == fast){
                key2 = slow;
                while(key1 != key2){
                    key1 = key1.next;
                    key2 = key2.next;
                }
                return key1;
            }
        }

        return null;
    }
}
